﻿using System;
using System.Collections.Generic;
using System.Text;
using Yuebon.Commons.Encrypt;
using Yuebon.Commons.IoC;
using Yuebon.Commons.Models;
using Yuebon.Shop.Dtos;
using Yuebon.Shop.IServices;
using Yuebon.Shop.Models;

namespace Yuebon.Shop.Application
{
    public class MembersApp
    {
        IMembersService service = IoCContainer.Resolve<IMembersService>();
        IMemberGradesService serviceMemberGrades = IoCContainer.Resolve<IMemberGradesService>();
        IMemberOpenIdsService serviceMemberOpenIds = IoCContainer.Resolve<IMemberOpenIdsService>();

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">是否降序</param>
        /// <returns>指定对象的集合</returns>
        public List<MembersDto> FindWithPager(string where, PagerInfo pagerInfo, string fieldToSort, bool desc)
        {
            List<Members> list = service.FindWithPager(where, pagerInfo, fieldToSort, desc);
            List<MembersDto> resultList = new List<MembersDto>();
            foreach (Members item in list)
            {
                MembersDto membersDto = new MembersDto();
                membersDto.Id = item.Id;

                membersDto.ReferralUserId = item.ReferralUserId;
                if (!string.IsNullOrEmpty(item.ReferralUserId))
                {
                    membersDto.ReferralUserName = service.Get(item.ReferralUserId).UserName;
                }

                membersDto.GradeId = item.GradeId;
                membersDto.GradeName = serviceMemberGrades.Get(item.GradeId).Name;
                membersDto.UserName = item.UserName;
                membersDto.PasswordQuestion = item.PasswordQuestion;
                membersDto.PasswordAnswer = item.PasswordAnswer;
                membersDto.CreateDate = item.CreateDate;
                membersDto.Email = item.Email;
                membersDto.RealName = item.RealName;
                membersDto.IdentityCard = item.IdentityCard;
                membersDto.Picture = item.Picture;
                membersDto.Gender = item.Gender;
                membersDto.BirthDate = item.BirthDate;
                membersDto.IsOpenBalance = item.IsOpenBalance;
                membersDto.OrderNumber = item.OrderNumber;
                membersDto.Expenditure = item.Expenditure;
                membersDto.Points = item.Points;
                membersDto.RequestBalance = item.RequestBalance;
                membersDto.Balance = item.Balance;
                membersDto.TopRegionId = item.TopRegionId;
                membersDto.RegionId = item.RegionId;
                membersDto.Address = item.Address;
                membersDto.CellPhone = item.CellPhone;
                membersDto.QQ = item.QQ;
                membersDto.Wangwang = item.Wangwang;
                membersDto.WeChat = item.WeChat;
                membersDto.EmailVerification = item.EmailVerification;
                membersDto.CellPhoneVerification = item.CellPhoneVerification;
                membersDto.RegisteredSource = item.RegisteredSource;
                membersDto.IsQuickLogin = item.IsQuickLogin;
                membersDto.IsLogined = item.IsLogined;
                membersDto.Unionid = item.Unionid;
                membersDto.IsSubscribe = item.IsSubscribe;
                membersDto.NickName = item.NickName;
                membersDto.TagIds = item.TagIds;
                membersDto.IsSendAppCoupons = item.IsSendAppCoupons;
                membersDto.ClientId = item.ClientId;
                membersDto.Token = item.Token;
                membersDto.StoreId = item.StoreId;
                membersDto.ShoppingGuiderId = item.ShoppingGuiderId;
                membersDto.ContactInfo = item.ContactInfo;
                membersDto.O2OStoreId = item.O2OStoreId;
                membersDto.IsDefaultDevice = item.IsDefaultDevice;
                //membersDto.OpenIdType =sev item.OpenIdType;
                //membersDto.OpenId = item.OpenId;
                resultList.Add(membersDto);
            }
            return resultList;
        }


        /// <summary>
        /// 查询会员是否存在
        /// </summary>
        /// <param name="username">会员用户名</param>
        /// <returns></returns>
        public Members GetMemberByUserName(string username)
        {
            return service.GetMemberByUserName(username);
        }


        /// <summary>
        /// 查询会员是否存在
        /// </summary>
        /// <param name="username">会员用户名</param>
        /// <returns></returns>
        public Members GetMemberByGradeId(string gradeId)
        {
            return service.GetMemberByUserName(gradeId);
        }


        /// <summary>
        /// 用户登陆验证。
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码（第一次md5加密后）</param>
        /// <returns>验证成功返回用户实体，验证失败返回null|提示消息</returns>
        public Tuple<Members, string> Validate(string userName, string password)
        {
            Members userEntity = service.GetMemberByUserName(userName);

            if (userEntity == null)
            {
                return new Tuple<Members, string>(null, "系统不存在该用户，请重新确认。");
            }
            string inputPassword = QQEncryptUtil.EncodePasswordWithVerifyCode(password, userEntity.PasswordSalt); ;

            if (inputPassword != userEntity.Password)
            {
                return new Tuple<Members, string>(null, "密码错误，请重新输入。");
            }
            else
            {
                return new Tuple<Members, string>(userEntity, "");
            }
        }
    }
}
